package com.nav.tictactoe;

import java.net.DatagramPacket;

public class Broadcaster implements Runnable {
	WaitActivity act;
	public Broadcaster(WaitActivity act) {
		this.act = act;
	}
	public void run() {
		try {
			String data = SharedData.gameName + "," + HelperFunctions.getLocalIpAddress() + "," + Configuration.ACCEPT_PORT;
			DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(),
				    HelperFunctions.getBroadcastAddress(act), Configuration.BROADCAST_PORT);
			while(!Thread.currentThread().isInterrupted() && SharedData.opponent == null) {
				SharedData.dtsocket.send(packet);
				if(!Thread.currentThread().isInterrupted() && SharedData.opponent == null)
					Thread.sleep(Configuration.BROADCAST_INTERVAL * 1000);
			}
		} catch(InterruptedException e) {
			// don't have to do anything, just return
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}
